import { defineStore } from "pinia";
import { ref } from "vue";
import { userGet, userLogin } from "../service/api/user";

export const useAccount = defineStore(
  "user",
  () => {
    const userList = ref([]); //账户列表
    const userInfo = ref(null); //登录成功后存放用户信息
    const initUserList = async () => {
      let results = await userGet();
      userList.value = results;
    };
    //触发登录请求，并修改userInfo
    const userLoginAction = async (user) => {
      let data = await userLogin(user);
      console.log("登录状态机", data);
      if (data.type == "success") {
        userInfo.value = data.userInfo;
      }
      return data;
    };

    const handleLogout = () => {
      userInfo.value = null;
    };
    return { userList, initUserList, userInfo, userLoginAction, handleLogout };
  },
  { persist: true }
);
